package com.xhwl.centiotdata.handler.security.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xhwl.centiotdata.handler.security.SecurityIntegrationFactory;
import com.xhwl.centiotdata.handler.security.SecurityIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.huawei.IOTConfigDTO;
import com.xhwl.centiotdata.suppliersystem.huawei.SecurityHuaWeiIoTDAHandler;
import com.xhwl.common.dto.cent.security.PerimeterActionDTO;
import com.xhwl.common.pojo.cent.security.PatrollingLine;
import com.xhwl.common.pojo.cent.security.PatrollingPlan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 10:51 2022/1/17
 */
@Slf4j
@Service
public class SecurityIntegrationHuaWeiIoTDAHandler extends SecurityIntegrationHandler {

    @Autowired
    SecurityHuaWeiIoTDAHandler securityHuaWeiIoTDAHandler;

    @Override
    public void afterPropertiesSet() throws Exception {
        SecurityIntegrationFactory.register(1, this);
    }

    @Override
    public Map<Boolean, List<String>> perimeterAction(IOTConfigDTO configDTO, PerimeterActionDTO perimeterActionDTO) {
        List<String> iotDeviceIdList = perimeterActionDTO.getIotDeviceIdList();
        Map<Boolean, List<String>> result = new HashMap<>();
        List<String> successList = new ArrayList<>();
        List<String> failList = new ArrayList<>();
        result.put(true, successList);
        result.put(false, failList);
        if (CollectionUtils.isNotEmpty(iotDeviceIdList)) {
            for (String iotDeviceId : iotDeviceIdList) {
                perimeterActionDTO.setIotDeviceId(iotDeviceId);
                boolean success = securityHuaWeiIoTDAHandler.perimeterAction(configDTO, perimeterActionDTO);
                if (success) {
                    successList.add(iotDeviceId);
                } else {
                    failList.add(iotDeviceId);
                }
            }
        }
        return result;
    }

    @Override
    public JSONObject onPushingPatrolsLine(IOTConfigDTO configDTO, PatrollingLine line, String oldName, List<String> nodeIds, String command) {
        return securityHuaWeiIoTDAHandler.onPushingPatrolsLine(configDTO, line, oldName, nodeIds, command);
    }

    @Override
    public JSONObject onPushingPatrolsPlan(IOTConfigDTO configDTO, PatrollingPlan plan, String command) {
        return securityHuaWeiIoTDAHandler.onPushingPatrolsPlan(configDTO, plan, command);
    }

    @Override
    public JSONObject onGettingCheckRecord(IOTConfigDTO configDTO, JSONObject params, String command) {
        return securityHuaWeiIoTDAHandler.onGettingCheckRecord(configDTO, params, command);
    }
}
